package sse.ustc.cc.custom;

import sse.ustc.cc.activity.R;
import sse.ustc.cc.util.CourseTime;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
/**
 * 添加课程时间段布局及响应事件
 * @author xuhao
 *
 */
public class AddCourseTimeView extends TableLayout {

	Context context;
	private TextView tv1 = null;//课程周数
	public TextView course_week = null;//选择课程周数
	private TextView tv3 = null;//课程节数
	public TextView course_jieshu = null;//选择课程节数
	private TextView tv5 = null;//课程时间
	public TextView course_time = null;//选择课程时间

	private TableRow tableRow1 = null;//课程周数行
	private TableRow tableRow2 = null;//课程节数行
	private TableRow tableRow3 = null;//课程时间行
	//private Button delTime = null;
	
	private CourseTime courseTime = null;//课程时间对象，即把用户选择的课程时间封装到一个CourseTime对象中

	public CourseTime getCourseTime() {
		return courseTime;
	}

	public void setCourseTime(CourseTime courseTime) {
		this.courseTime = courseTime;
	}

	private Context getMyContext() {
		return context;
	}
	/**
	 * 把pix单位转化为dp
	 * @param pixels 像素
	 * @return dp数
	 */
	public int getdip(int pixels) {

		float scale = getMyContext().getResources().getDisplayMetrics().density;

		return (int) (pixels * scale + 0.5f);

	}

	public AddCourseTimeView(Context context) {
		super(context);
		this.context = context;
		//TableLayout布局器
		TableLayout.LayoutParams tableLayoutParams = new TableLayout.LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
		tableLayoutParams.topMargin = 20;//TableLayout Margin值
		setLayoutParams(tableLayoutParams);

		setClickable(true); // 可点击
		setFocusable(true); // 可聚焦
		setOrientation(LinearLayout.VERTICAL);
		//实例化控件
		
		tv1 = new TextView(context);
		course_week = new TextView(context);
		tv3 = new TextView(context);
		course_jieshu = new TextView(context);
		tv5 = new TextView(context);
		course_time = new TextView(context);
		//TableRow 布局器
		TableRow.LayoutParams rowparams = new TableRow.LayoutParams();
		tv1.setText("上课周数：");
		tv1.setTextSize(20);
		tv1.setHeight(getdip(40));
		// tv1.setWidth(180);
		course_week.setWidth(getdip(150));
		course_week.setTextSize(20);
		course_week.setSingleLine(true);
		// course_week.setGravity(Gravity.CENTER);
		course_week.setText("选择上课周数");
		tableRow1 = new TableRow(context);
		tableRow1.setOrientation(LinearLayout.HORIZONTAL);
		tableRow1.setLayoutParams(rowparams);
		tableRow1.setGravity(Gravity.CENTER_HORIZONTAL);
		tableRow1.addView(tv1);
		tableRow1.addView(course_week);

		tv3.setText("上课节数：");
		tv3.setTextSize(20);
		tv3.setHeight(getdip(40));
		// tv3.setWidth(180);
		course_jieshu.setWidth(getdip(150));
		course_jieshu.setTextSize(20);
		course_jieshu.setSingleLine(true);
		// course_jieshu.setGravity(Gravity.CENTER);
		course_jieshu.setText("选择上课节数");
		tableRow2 = new TableRow(context);
		tableRow2.setOrientation(LinearLayout.HORIZONTAL);
		tableRow2.setGravity(Gravity.CENTER_HORIZONTAL);
		tableRow2.setLayoutParams(rowparams);
		tableRow2.addView(tv3);
		tableRow2.addView(course_jieshu);

		tv5.setText("上课时间：");
		tv5.setTextSize(20);
		tv5.setHeight(getdip(40));
		// tv1.setWidth(180);
		course_time.setWidth(getdip(150));
		course_time.setTextSize(20);
		course_time.setSingleLine(true);
		// course_time.setGravity(Gravity.CENTER);
		course_time.setText("选择上课时间");
		tableRow3 = new TableRow(context);
		tableRow3.setOrientation(LinearLayout.HORIZONTAL);
		tableRow3.setGravity(Gravity.CENTER_HORIZONTAL);
		tableRow3.setLayoutParams(rowparams);
		tableRow3.addView(tv5);
		tableRow3.addView(course_time);
		//将tablerow添加到tablelayout中
		addView(tableRow1);
		addView(tableRow2);
		addView(tableRow3);

		/**
		 * 添加课程周数
		 */
		course_week.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// 如果courseTime为空，则实例化
				if (courseTime == null) {
					courseTime = new CourseTime();
				}
				// 初始化一个自定义的选择课程周数Dialog
				final WeekChooseDialog dialog = new WeekChooseDialog(getMyContext());
				dialog.setTitle("选择上课周数");

				dialog.show();
				//得到确定按钮
				Button btnok = (Button) dialog.getWindow().findViewById(
						R.id.weekchoosedok);
				//得到取消按钮
				Button btnCancle = (Button) dialog.getWindow().findViewById(
						R.id.weekchoosedcancle);
				//为确定按钮绑定响应事件
				btnok.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						// 消除dialog
						dialog.dismiss();
						// 得到选择的周数
						courseTime.setStartWeek(dialog.getStartWeekNum());
						courseTime.setEndWeek(dialog.getEndWeekNum());
						course_week.setText(dialog.getStartWeekNum() + " ~ "
								+ dialog.getEndWeekNum() + "周");
					}
				});
				//为取消按钮绑定响应事件
				btnCancle.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						dialog.dismiss();
					}
				});

			}
		});

		course_jieshu.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// 如果courseTime为空，则实例化
				if (courseTime == null) {
					courseTime = new CourseTime();
				}
				// 初始化一个自定义的Dialog
				final SectionChooseDialog dialog = new SectionChooseDialog(
						getMyContext());
				dialog.setTitle("选择上课节数");

				dialog.show();
				Button btnok = (Button) dialog.getWindow().findViewById(
						R.id.jieshuchoosedok);
				Button btnCancle = (Button) dialog.getWindow().findViewById(
						R.id.jieshuchoosedcancle);
				btnok.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						dialog.dismiss();
						courseTime.setWeekJieShu(dialog.getWeekChoosed());
						courseTime.setStartJieShu(dialog.getJieshuStartNum());
						courseTime.setEndJieShu(dialog.getJieshuEndNum());
						course_jieshu.setText(dialog.getWeekChoosed() + "  "
								+ dialog.getJieshuStartNum() + " ~ "
								+ dialog.getJieshuEndNum() + "节");
					}
				});
				btnCancle.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						dialog.dismiss();
					}
				});

			}
		});

		course_time.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// 如果courseTime为空，则实例化
				if (courseTime == null) {
					courseTime = new CourseTime();
				}
				// 初始化一个自定义的Dialog
				final TimeChooseDialog dialog = new TimeChooseDialog(
						getMyContext());
				dialog.setTitle("选择上课节数");

				dialog.show();
				Button btnok = (Button) dialog.getWindow().findViewById(
						R.id.timechoosedok);
				Button btnCancle = (Button) dialog.getWindow().findViewById(
						R.id.timechoosedcancle);
				btnok.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						dialog.dismiss();
						courseTime.setStartTime(dialog.getStarthour() + ":"
								+ dialog.getStartMinute());
						courseTime.setEndTime(dialog.getEndHour() + ":"
								+ dialog.getEndMinute());
						course_time.setText(dialog.getStarthour() + ":"
								+ dialog.getStartMinute() + " ~ "
								+ dialog.getEndHour() + ":"
								+ dialog.getEndMinute());
					}

				});

				btnCancle.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						dialog.dismiss();
					}
				});
			}
		});
	}

}
